﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace Library.Linq.Expressions
{
    public class ContainsExpression : Expression
    {
        internal ContainsExpression(Expression container, Expression element, MethodInfo method)
        {
            this.container = container;
            this.element = element;
            this.method = method ?? container.Type.GetMethod("Contains", new Type[] { element.Type });
        }

        Expression container;
        Expression element;
        MethodInfo method;

        public Expression Container
        {
            get
            {
                return container;
            }
        }
        public Expression Element
        {
            get
            {
                return element;
            }
        }
        public MethodInfo Method
        {
            get
            {
                return method;
            }
        }

        public override ExpressionType NodeType
        {
            get
            {
                return ExpressionType.Extension;
            }
        }
        public override Type Type
        {
            get
            {
                return typeof(bool);
            }
        }
        public override bool CanReduce
        {
            get
            {
                return true;
            }
        }

        public override Expression Reduce()
        {
            if (method.IsStatic)
            {
                return Expression.Call(null, method, container, element);
            }
            else
            {
                return Expression.Call(container, method, element);
            }
        }
    }
}
